<template>
	<view>
		<view class="navbar acea-row row-around">
			<view class="item acea-row row-center-wrapper" :class="{ on: navOn === 0 }" @click="onNav(0)">未使用</view>
			<view class="item acea-row row-center-wrapper" :class="{ on: navOn === 1 }" @click="onNav(1)">已使用</view>
			<view class="item acea-row row-center-wrapper" :class="{ on: navOn === 2 }" @click="onNav(2)">过期</view>
		</view>
		<view class='coupon-list' v-if="couponsList.length">
			<view class='item acea-row row-center-wrapper' v-for='(item,index) in couponsList' :key="index"
				:class="{svip: item.receive_type === 4}" >
				<view class='money' :class='item._type == 0 ? "moneyGray" : ""'>
					<view>￥<text class='num'>{{item.coupon_price}}</text></view>
					<view class="pic-num" v-if="item.use_min_price > 0">满{{item.use_min_price}}元可用</view>
					<view class="pic-num" v-else>红包券</view>
				</view>
				<view class='text rel'>
					<view class='condition'>
						<view class="name line2">
							<view class="line-title" :class="item._type === 0 ? 'bg-color-huic' : 'bg-color-check'"
								v-if="item.applicable_type === 0">通用劵</view>
							<view class="line-title" :class="item._type === 0 ? 'bg-color-huic' : 'bg-color-check'"
								v-else-if="item.applicable_type === 1">品类券</view>
							<view class="line-title" :class="item._type === 0 ? 'bg-color-huic' : 'bg-color-check'"
								v-else>商品券</view>
							<image src="../../../static/images/fvip.png" class="pic" v-if="item.receive_type===4">
							</image>
							<text>{{item.coupon_title}}</text>
						</view>
					</view>
					
					<!-- <view style="color:#ccc;font-size:20rpx;">券ID: {{item.id}}</view> -->
					<view style="color:#ccc;font-size:20rpx;"><image :src="item.avatar" style="width:40rpx;height:40rpx;border-radius: 1rem;float: left;"><view style="padding: 4px;">{{item.nickname}}</view></view>

					<button class=" share-bbtn" open-type="share" hover-class='none' :id="item.id"  >
						<view class="view flex-row align-center" >
							<!-- <view class="iconfont icon-weixin3" style="color:#63d678;"></view> -->
							<image src="https://videocun.oss-cn-beijing.aliyuncs.com/7da2d202202102240069700.png" style="width:40rpx;height:40rpx;">
						</view>
						<view style="font-size:24rpx;color:#fff;">分享赚钱</view>
					</button>


					<view class='data acea-row row-between-wrapper bot-trow'>
						<view class='bnt gray' v-if="item._type==0">{{item._msg}}</view>
						<view style="font-size:24rpx;" v-else>{{item._msg}}</view>
						<view>{{item.add_time}}-{{item.end_time}}</view>
					</view>

				</view>
			</view>
		</view>
		<view class='noCommodity' v-else>
			<view class='pictrue'>
				<image src='https://videocun.oss-cn-beijing.aliyuncs.com/904c7202202102214065519.png'></image>
			</view>
		</view>
		<!-- #ifdef MP -->
		<!-- <authorize @onLoadFun="onLoadFun" :isAuto="isAuto" :isShowAuth="isShowAuth" @authColse="authColse"></authorize> -->
		<!-- #endif -->
		<home></home>
	</view>
</template>

<script>
	import {
		getUserCoupons
	} from '@/api/api.js';
	import {
		couponsShare
	} from '@/api/activity.js';
	import {
		toLogin
	} from '@/libs/login.js';
	import {
		mapGetters
	} from "vuex";
	// #ifdef MP
	import authorize from '@/components/Authorize';
	// #endif
	import home from '@/components/home';
	export default {
		components: {
			// #ifdef MP
			authorize,
			// #endif
			home
		},
		data() {
			return {
				couponsList: [],
				loading: false,
				isAuto: false, //没有授权的不会自动授权
				isShowAuth: false, //是否隐藏授权
				navOn: 0,
				page: 1,
				limit: 15,
				couPrice: 0,
				cpuid: '', // 选中的红包券id
				finished: false
			};
		},
		computed: mapGetters(['isLogin']),
		watch: {
			isLogin: {
				handler: function(newV, oldV) {
					if (newV) {
						this.getUseCoupons();
					}
				},
				deep: true
			}
		},
		onLoad() {
			if (this.isLogin) {
				this.getUseCoupons();
			} else {
				toLogin();
			}
		},
		onShareAppMessage(res) {
			// var userId = uni.getStorageSync('shareUserId');
			// if (userId) this.share.path = '/pages/index/index?parentId=' + parseInt(userId);
			// console.log(this.share.path)
			
			const ret = this.couponsList.find( item => item.id == res.target.id)
			this.couPrice = ret.coupon_price
			let that = this
			couponsShare(ret.id)
			return {
				// title: `送你一张${this.couPrice}元的红包券，点一下就能领`,
				title: `送你一张现金红包券，点一下就能领！`,
				path: '/pages/activity/redbackpacket/index?cpuid='+ret.id ,
				imageUrl: 'https://videocun.oss-cn-beijing.aliyuncs.com/06aa1202202251744475364.jpg',
				success:  res => {
					console.log(res)
				},
				fail(res) {
					uni.showToast({
						title: '分享失败',
						icon: 'none'
					})
				}
			}
			
		},
		onShareTimeline(res) {
			let that = this
			// couponsShare({cpuid: that.cpuid})
			
			console.log(that.cpuid)
			// return {
			// 	title: `送你一张${this.couPrice}的红包券，点一下就能领`,
			// 	path: '/pages/activity/redbackpacket/index',
			// 	imageUrl: 'https://videocun.oss-cn-beijing.aliyuncs.com/6d37c202202120007292798.png',
			// 	success(res) {
			// 		// uni.showToast({
			// 		// 	title: '分享成功'
			// 		// })
					
			// 	},
			// 	fail(res) {
			// 		// uni.showToast({
			// 		// 	title: '分享失败',
			// 		// 	icon: 'none'
			// 		// })
			// 	}
			// }
		},
		onReachBottom() {
			this.getUseCoupons();
		},
		methods: {
			getCouPirce(item) {
				this.cpuid = item.id
				
			},
			onNav: function(type) {
				this.navOn = type;
				this.couponsList = [];
				this.page = 1;
				this.finished = false;
				this.getUseCoupons();
			},
			useCoupon(item){
				let url = '';
				if (item.category_id == 0 && item.product_id == '') {
					url = '/pages/goods_list/index?title=默认'
				}
				if (item.category_id != 0) {
					if (item.category_type == 1) {
						url = '/pages/goods_list/index?cid='+item.category_id+'&title='+item.category_name
					}else{
						url = '/pages/goods_list/index?sid='+item.category_id+'&title='+item.category_name
					}
				}
				if (item.product_id != '') {
					let arr = item.product_id.split(',');
					let num = arr.length;
					if (num == 1) {
						url = '/pages/goods_details/index?id='+item.product_id
					} else {
						url = '/pages/goods_list/index?productId='+item.product_id+'&title=默认'
					}
				}
				uni.navigateTo({
					url: url
				});
			},
			/**
			 * 授权回调
			 */
			onLoadFun: function() {
				this.getUseCoupons();
			},
			// 授权关闭
			authColse: function(e) {
				this.isShowAuth = e
			},
			/**
			 * 获取领取红包券列表
			 */
			getUseCoupons: function() {
				let that = this;
				if (that.loading || that.finished) {
					return;
				}
				that.loading = true;
				uni.showLoading({
					title: '正在加载…'
				});
				getUserCoupons(this.navOn, {
					page: this.page,
					limit: this.limit
				}).then(res => {
					that.loading = false;
					uni.hideLoading();
					that.couponsList = that.couponsList.concat(res.data);
					that.finished = res.data.length < that.limit;
					that.page += 1;
				}).catch(err => {
					that.loading = false;
					uni.showToast({
						title: err,
						icon: 'none'
					});
				});
			}
		}
	}
</script>

<style>
	.share-bbtn {
		height: 50rpx;
		width: 160rpx;
		position: absolute;
		right: 0;
		top: 110rpx;
		display: flex;
		background: rgba(232, 51, 35, 1);
		justify-content: center;
		align-items: center;
		margin-right: 0;
		margin-top: -6rpx;
		border-top-left-radius: 30rpx;
		border-bottom-left-radius: 30rpx;
	}
	.rel {position: relative;}
	.bot-trow {
		position: absolute;
		bottom: 5px;
		width: 400rpx;
	}
	.money {
		display: flex;
		flex-direction: column;
		justify-content: center;
	}

	.pic-num {
		color: #ffffff;
		font-size: 24rpx;
	}

	.coupon-list .item .text .condition {
		display: flex;
		align-items: center;
	}

	.coupon-list .item .text .condition .name {
		font-size: 26rpx;
		font-weight: 500;
	}

	.coupon-list .item .text .condition .pic {
		width: 30rpx;
		height: 30rpx;
		display: block;
		margin-right: 10rpx;
		display: inline-block;
		vertical-align: middle;
	}

	.condition .line-title {
		width: 70rpx;
		height: 32rpx !important;
		line-height: 30rpx;
		text-align: center;
		box-sizing: border-box;
		background: rgba(255, 247, 247, 1);
		border: 1px solid rgba(232, 51, 35, 1);
		opacity: 1;
		border-radius: 20rpx;
		font-size: 18rpx !important;
		color: #e83323;
		margin-right: 12rpx;
		text-align: center;
		display: inline-block;
		vertical-align: middle;
	}

	.condition .line-title.bg-color-huic {
		border-color: #BBB !important;
		color: #bbb !important;
		background-color: #F5F5F5 !important;
	}
</style>

<style lang="scss" scoped>
	.navbar {
		position: fixed;
		top: 0;
		left: 0;
		width: 100%;
		height: 106rpx;
		background-color: #FFFFFF;
		z-index: 9;

		.item {
			border-top: 5rpx solid transparent;
			border-bottom: 5rpx solid transparent;
			font-size: 30rpx;
			color: #999999;

			&.on {
				border-bottom-color: #E93323;
				color: #282828;
			}
		}
	}

	.coupon-list {
		margin-top: 122rpx;
	}

	.noCommodity {
		margin-top: 300rpx;
	}
</style>
